<template>
	<!-- 碎片详情 -->
	<view>
		<!-- #ifdef APP-PLUS -->
		<view class="status_bar"></view>  
		<!-- #endif -->
		<navbar title="碎片详情" />
		<view class="list">
			<view class="main" v-for="(item,index) in sgoods" :key="index">
				<image :src="item.value"></image>
				<view class="title">
					{{index+1}}号碎片
					<text>x{{item.num}}</text>
				</view>
			</view>
		</view>
		<view class="btns">
			<view class="btn" @click="synthesis">合成商品订单</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				goods_id:'',
				goods_ids:[],
				activity_id:'',
				mysgoods:[],
				sgoods:[]
			}
		},
		onLoad(options) {
			console.log("options: ",options);
			this.goods_id = options.goods_id;
			this.activity_id = options.activity_id;
			this.getSgoodsdetail();
		},
		methods: {
			//获取碎片详情
			getSgoodsdetail(){
				this.$api.sgoodsdetail({
					goods_id:this.goods_id,
					activity_id:this.activity_id
				}).then(res=>{
					this.mysgoods = res.my_s_goods;
					this.sgoods = res.s_goods;
					for(let i=0;i<this.sgoods.length;i++){
						let num = 0;
						for(let j=0;j<this.mysgoods.length;j++){
							if(this.sgoods[i].id == this.mysgoods[j].s_goods_id){
								num++;
								this.sgoods[i].num = num;
							}else{
								this.sgoods[i].num = num;
							}
						}
					}
					let mysgoodsid = [];
					for(let n=0;n<this.mysgoods.length;n++){
						if(mysgoodsid.indexOf(this.mysgoods[n].s_goods_id) === -1){
							mysgoodsid.push(this.mysgoods[n].s_goods_id);
							this.goods_ids.push(this.mysgoods[n].id);
						}
					}
				})
			},
			//合成商品订单
			synthesis(){
				let sgoods = this.sgoods.every(item => {
					return item.num > 0
				})
				if(sgoods){
					uni.redirectTo({
						url:'/pages/package/takedelivery?' + this.$url.urlEncode({
							goods_id:this.goods_id,
							goods_ids:this.goods_ids.toString(),
							pageType:'synthesis'
						})
					})
				}else{
					uni.showToast({
						title:'您尚未集齐合成商品所需碎片',
						icon:'none'
					})
				}
			}
		}
	}
</script>

<style lang="scss">
page{
	padding: calc(var(--status-bar-height) + 100rpx) 0 30rpx;
}
.list{
	margin: 50rpx 30rpx;
	display: flex;
	flex-wrap: wrap;
	
	.main{
		width: 218rpx;
		height: 298rpx;
		background: #FBFBFB;
		border-radius: 10rpx;
		margin: 0 16rpx 30rpx 0;
		
		&:nth-child(3n){
			margin-right: 0;
		}
		
		image{
			width: 218rpx;
			height: 218rpx;
		}
		.title{
			width: 190rpx;
			font-size: 28rpx;
			font-family: Source Han Sans CN;
			font-weight: bold;
			color: #333333;
			line-height: 32rpx;
			@include two;
			@include between;
			margin: 15rpx 20rpx 15rpx 10rpx;
			
			text{
				color: #f00;
			}
		}
	}
}
.btns{
	width: 748rpx;
	height: 100rpx;
	background: #FFFFFF;
	@include center;
	position: fixed;
	bottom: 0;
	z-index: 999;
	
	.btn{
		width: 634rpx;
		height: 70rpx;
		background: linear-gradient(180deg, #FFD598 0%, #FF6763 100%);
		border-radius: 35rpx;
		font-size: 24rpx;
		font-family: Source Han Sans CN;
		font-weight: 400;
		color: #FFFFFF;
		@include center;
	}
}
</style>
